package com.ailanyin.modules.flowable.controller;

import cn.hutool.json.JSONUtil;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.modules.flowable.domain.bo.WfProcessBo;
import com.ailanyin.modules.flowable.domain.vo.WfDeployVo;
import com.ailanyin.modules.flowable.domain.vo.WfFormVo;
import com.ailanyin.modules.flowable.service.IWfDeployFormService;
import com.ailanyin.modules.flowable.service.IWfDeployService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * 流程部署
 *
 * @author KonBAI
 * @createTime 2022/3/24 20:57
 */
@Slf4j
@RestController
@RequestMapping("/workflow/deploy")
public class WfDeployController {

    @Autowired
    private IWfDeployService deployService;
    @Autowired
    private IWfDeployFormService deployFormService;

    /**
     * 查询流程部署列表
     */
    @GetMapping("/list")
    public CommonPage<WfDeployVo> list(WfProcessBo processBo,
                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return deployService.queryPageList(processBo, pageNum, pageSize);
    }

    /**
     * 查询流程部署版本列表
     */
    @GetMapping("/publishList")
    public CommonPage<WfDeployVo> publishList(@RequestParam String processKey,
                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return deployService.queryPublishList(processKey, pageNum, pageSize);
    }

    /**
     * 激活或挂起流程
     *
     * @param state        状态（active:激活 suspended:挂起）
     * @param definitionId 流程定义ID
     */
    @PutMapping(value = "/changeState")
    public AjaxResult changeState(@RequestParam String state, @RequestParam String definitionId) {
        deployService.updateState(definitionId, state);
        return AjaxResult.success();
    }

    /**
     * 读取xml文件
     *
     * @param definitionId 流程定义ID
     * @return
     */
    @GetMapping("/bpmnXml/{definitionId}")
    public AjaxResult getBpmnXml(@PathVariable(value = "definitionId") String definitionId) {
        return AjaxResult.success(null, deployService.queryBpmnXmlById(definitionId));
    }

    /**
     * 删除流程模型
     *
     * @param deployIds 流程部署ids
     */
    @DeleteMapping("/{deployIds}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] deployIds) {
        deployService.deleteByIds(Arrays.asList(deployIds));
        return AjaxResult.success();
    }

    /**
     * 查询流程部署关联表单信息
     *
     * @param deployId 流程部署id
     */
    @GetMapping("/form/{deployId}")
    public AjaxResult start(@PathVariable(value = "deployId") String deployId) {
        WfFormVo formVo = deployFormService.selectDeployFormByDeployId(deployId);
        if (Objects.isNull(formVo)) {
            return AjaxResult.error("请先配置流程表单");
        }
        return AjaxResult.success(JSONUtil.toBean(formVo.getContent(), Map.class));
    }
}
